import Common from "../Common";
import {bcComp, isEmpty} from "../common/Helpers";

const {ccclass, property} = cc._decorator;

@ccclass
export default class RechargeDialog extends Common {


    @property(cc.Node)
    confirmBtn: cc.Node;

    @property(cc.Label)
    amountLabel: cc.Label;


    protected formData: any = {
        money: '',
        desc: '',
    }
    protected watch = {
        userInfo: (val) => {
            const account = val.account || {}
            this.amountLabel.string = account.cash_account || 0.00
        }
    }

    start() {
        this.confirmBtn.on(cc.Node.EventType.TOUCH_START, this.onRecharge, this)
    }


    async onRecharge() {
        this.onBtnClick()
        const formData = this.formData;
        if (bcComp(formData.money, 0) < 1) {
            return this.showToast('请输入合法的金额')
        }
        if (isEmpty(formData.desc)) {
            return this.showToast('请输入充值备注')
        }
        try {
            const {message} = await this.http.post('recharge/recharge', formData)
            this.showToast(message);
            this.node.active = false
        } catch ({message}) {
            this.showToast(message);
        }
    }

}
